package com.icat.house.web.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.icat.house.common.CaptchaRender;
import com.icat.house.common.Constant;
import com.icat.house.model.HouseInfo;
import com.icat.house.model.UserInfo;
import com.icat.house.web.controller.BaseController;
import com.icat.house.web.intercept.AdminInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;

@ControllerBind(controllerKey = "/admin", viewPath = "/page/admin/")
public class AdminController extends BaseController {
	@Before(AdminInterceptor.class)
	public void index() {
		render("index.html");
	}

	@ActionKey("/admin/login")
	public void login() {
		render("login.html");
	}

	@ActionKey("/admin/login/post")
	public void adminLogin() {
		String idCard = getPara("idCard", "");
		String password = getPara("password", "");
		String captcha = getPara("captcha", "");
		String r = getPara("r");
		if (!CaptchaRender.validate(getSessionAttr(r), captcha)) {
			setAttr(Constant.ERROR, Constant.CAPTCHA_ERROR);
			forwardAction("/admin/login");
			return;
		}
		UserInfo user = UserInfo.dao.isUser(idCard, password);
		if (!StrKit.notNull(user)) {
			setAttr(Constant.ERROR, Constant.ERROR_USER_PASSWOED_ERROR);
			forwardAction("/admin/login");
			return;
		}
		int status = user.getInt("status");
		if (status != 1) {
			setAttr(Constant.ERROR, Constant.USER_NO_ADMIN);
			forwardAction("/admin/login");
			return;
		}
		setAttr(Constant.KEY_USER, JSONObject.parseObject(JsonKit.toJson(user)));
		render("index.html");
	}

	@ActionKey("/admin/user/list")
	public void userList() {
		setAttr(Constant.DATA, JsonKit.toJson(UserInfo.dao.userList(getParaToInt("select", 0), getPara("param", ""))));
		render("userIndex.html");
	}

	@ActionKey("/admin/user/delete")
	public void userDelete() {
		if (UserInfo.dao.deleteById(getParaToInt("id"))) {
			setAttr(Constant.MESSAGE, Constant.SUCCESS_MESSAGE_DELETE);
		} else {
			setAttr(Constant.MESSAGE, Constant.ERROR_MESSAGE_DELETE);
		}
		forwardAction("/admin/user/list");
	}

	@ActionKey("/admin/user/add/view")
	public void userAddView() {
		render("userAdd.html");
	}

	@ActionKey("/admin/user/add")
	public void userAdd() {
		String nick_name = getPara("nick_name");
		String phone = getPara("phone");
		String id_card = getPara("id_card");
		boolean result = false;
		int status = getParaToInt("status");
		String password = "123456";
		if (status == 0) {
			password = id_card.substring(id_card.length() - 6, id_card.length());
		}
		result = UserInfo.dao.save(nick_name, password, phone, id_card, null, status);
		if (result) {
			setAttr(Constant.MESSAGE, Constant.SUCCESS_MESSAGE_SAVE);
		} else {
			setAttr(Constant.MESSAGE, Constant.ERROR_MESSAGE_SAVE);
		}
		forwardAction("/admin/user/list");
	}

	@ActionKey("/admin/user/edit/view")
	public void userEditView() {
		setAttr(Constant.KEY_USER, JSONObject.parseObject(JsonKit.toJson(UserInfo.dao.findById(getParaToInt("id")))));
		render("userEdit.html");
	}

	@ActionKey("/admin/user/edit")
	public void userEdit() {
		String nick_name = getPara("nick_name");
		String phone = getPara("phone");
		String id_card = getPara("id_card");
		UserInfo userInfo = UserInfo.dao.findById(getParaToInt("id"));
		userInfo.set("nick_name", nick_name);
		userInfo.set("phone", phone);
		userInfo.set("id_card", id_card);
		if (userInfo.update()) {
			setAttr(Constant.MESSAGE, Constant.SUCCESS_MESSAGE_UPDATE);
		} else {
			setAttr(Constant.MESSAGE, Constant.ERROR_MESSAGE_UPDATE);
		}
		forwardAction("/admin/user/list");
	}

	@ActionKey("/admin/house/list")
	public void houseList() {
		String param = getPara("param");
		int status = getParaToInt("status", -1);
		setAttr(Constant.DATA, JSONArray.parseArray(JsonKit.toJson(HouseInfo.dao.findByParam(param, status))));
		render("houseIndex.html");
	}

	@ActionKey("/admin/house/delete")
	public void houseDelete() {
		if (HouseInfo.dao.deleteById(getParaToInt("id"))) {
			setAttr(Constant.MESSAGE, Constant.SUCCESS_MESSAGE_DELETE);
		} else {
			setAttr(Constant.MESSAGE, Constant.ERROR_MESSAGE_DELETE);
		}
		forwardAction("/admin/house/list");
	}

	@ActionKey("/admin/house/add/view")
	public void houseAddView() {
		render("houseAdd.html");
	}

	@ActionKey("/admin/house/add")
	public void houseAdd() {
		String room_no = getPara("room_no");
		int buildings = getParaToInt("buildings", 0);
		int unit = getParaToInt("unit", 0);
		String acreage = getPara("acreage");
		String price = getPara("price");
		String adviser = getPara("adviser");
		boolean result = HouseInfo.dao.save(room_no, buildings, unit, Double.valueOf(acreage), Double.valueOf(price),
				adviser);
		if (result) {
			setAttr(Constant.MESSAGE, Constant.SUCCESS_MESSAGE_SAVE);
		} else {
			setAttr(Constant.MESSAGE, Constant.ERROR_MESSAGE_SAVE);
		}
		forwardAction("/admin/house/list");
	}

	@ActionKey("/admin/house/edit/view")
	public void houseEditView() {
		setAttr(Constant.DATA, JSONObject.parseObject(JsonKit.toJson(HouseInfo.dao.findById(getParaToInt("id")))));
		render("houseEdit.html");
	}

	@ActionKey("/admin/house/edit")
	public void houseEdit() {
		String room_no = getPara("room_no");
		int buildings = getParaToInt("buildings", 0);
		int unit = getParaToInt("unit", 0);
		String acreage = getPara("acreage");
		String price = getPara("price");
		String adviser = getPara("adviser");
		int id = getParaToInt("id");
		HouseInfo houseInfo = HouseInfo.dao.findById(id);
		houseInfo.set("room_no", room_no);
		houseInfo.set("buildings", buildings);
		houseInfo.set("unit", unit);
		houseInfo.set("acreage", acreage);
		houseInfo.set("price", price);
		houseInfo.set("total_price", Double.valueOf(price) * Double.valueOf(acreage));
		houseInfo.set("adviser", adviser);
		if (houseInfo.update()) {
			setAttr(Constant.MESSAGE, Constant.SUCCESS_MESSAGE_UPDATE);
		} else {
			setAttr(Constant.MESSAGE, Constant.ERROR_MESSAGE_UPDATE);
		}
		forwardAction("/admin/house/list");
	}
}
